home *** CD-ROM | disk | FTP | other *** search
- Path: apccorp.apcc.com!root
- From: nfegan@apcc.com (Noel Fegan)
- Newsgroups: comp.lang.c++
- Subject: Re: Major problem with strings.
- Date: Tue, 12 Mar 1996 10:44:44 GMT
- Organization: American Power Conversion
- Message-ID: <4i3kkg$bar@apccorp.apcc.com>
- References: <31438275.72DB@aol2.com>
- NNTP-Posting-Host: hewie.galway.apcc.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- Neil <neil@aol2.com> wrote:
-
- >Here's my code:
-
- >1 char *club="";
- >2 club="/public_html/neil";
-
- >3 strcat(club,argv[1]+5);
-
- >4 strcat(club,"/");
-
- >----------------------------------------
- >(lines are numbered for reference only)
-
- >the argv[1] looks like: "test=12345"
-
- >So, ultimately, I want club to look like: "/public_html/neil12345"
-
- You have made a fundamental mistake here.
-
- Line 1 sets the pointer "club" to point to an empty string. On the next line you
- change the value of "club" to point to a different string, making the first line
- redundant. Neither of these are wrong, but it indicates to me that you do not
- fully understand what is happening when you have a line like char * club="". The
- pointer club is set to the value of a memory address which contains a '\0'
- character. The variable is not set to be a '\0' character. club could be a value
- like 0x4DD3, a memory address which happen to contain '\0' character.
-
- On line 2 the value of club is changed to point somewhere else in memory. This
- time the memory address pointed to contains a '/' character and the memory
- address 1 byte after this contains a 'p' character and so on until we have a
- string "/public_html/neil/". The final '/' character is followed by a '\0'
- character which terminates the string.
-
- Line 3 is where you make your first mistake. The memory address pointed to by
- club after line 2 is a block of static memory which is automatically allocated
- by the program. The block of memory is exactly 17 bytes long (including the '\0'
- character). When you do a strcat you are adding to the end of the block pointed
- by club, which in this case is a block of memory which is not large enough to
- hold any more data. The strcat function sets the '\0' character at the end of
- the block to the '1' character. It then proceeds to write '2', '3' ... in memory
- space which it has no right access at all. So the "2345\0" characters are
- actaully written in memory In which it is not allowed to write.
-
- Line 4 just adds to the mistake by writing 1 more byte into unowned memory. The
- consequence of writing in unowned territory depends on where the memory happens
- to be, the operating system, and other such things, suffice it to say that the
- results are unpredictable.
-
- What you should do is the following:
-
- //...
- char * pStub = "/public_html/neil";
- char * pExtra = (char *) argv[1]+5;
- char * pNewChars = new char[strlen(pStub) + strlen(pExtra)+1];
-
- strcpy(pNewChars, pStub);
- strcat(pNewChars, pExtra);
- //...
-
- The line "char * pNewChars = new char[strlen(pStub) + strlen(pExtra)+1];"
- allocates a buffer which is just big enough for the stub with the extra adds to
- it and also allows for the '\0' at the end of the string.
-
- The code assumes that the string passed in to the program, pointed to by argv[1]
- has at least 5 characters in the string. The program should check for correct
- parameter before blindly assuming that the user typed in the correct data. If
- the string pointed to by argv[1] happens to be shorter than 5 characters long
- (not including the '\0' character) then this program could have a serious bug.
-
-
- --
- Noel Fegan
- European Software Development Department
- American Power Conversion
- I don't speak for APC...
- nfegan@apcc.com
-
-